
png("figures/raw.png", 
    width = 2400,    
    height = 1800, res = 300)     
panelview(Y ~ W, data = data,index = c("State","Year"),type = "raw")
dev.off()

col <- ifelse(
  as.numeric(data$Year) %% 4 == 0,
  rgb(175/255, 59/255, 110/255, alpha = 0.3),
  rgb(10/255, 135/255, 84/255, alpha = 0.3) 
)

VEP <- as.numeric(gsub(",", "", data[data$TOTAL_BALLOTS_COUNTED != "",]$VEP))
total <- as.numeric(gsub(",", "", data[data$TOTAL_BALLOTS_COUNTED != "",]$TOTAL_BALLOTS_COUNTED)) * 100 / VEP
highest_office <- as.numeric(gsub(",", "", data[data$TOTAL_BALLOTS_COUNTED != "",]$VOTE_FOR_HIGHEST_OFFICE)) * 100 / VEP
col = col[data$TOTAL_BALLOTS_COUNTED != ""]


correlation <- lm(total ~ highest_office)

png("figures/correlation.png", width = 2400, height = 2400, res = 300)
par(mgp = c(1.5,0.5,0), mar = c(2.6,2.6,0.1,0.1))
plot(
  highest_office, total,
  xlab = "Turnout With Vote for Highest Office (%)",
  ylab = "Turnout With Total Ballots Counted (%)",
  pch = 19,
  col = col
)
abline(correlation, col = "#03254E", lwd = 2, lty = "dotted")
legend(
  "topleft",
  legend = c("Presidential Elections", 
             "Midterm Elections",
             "Line of Best Fit"),
  pch    = c(19, 19, NA),                         # NA for the line entry
  col    = c(rgb(175/255,59/255,110/255,0.3),
             rgb(10/255,135/255,84/255,0.3),
             "#03254E"),                          # line color
  lty    = c(NA, NA, "dotted"),                   # only the 3rd gets a line
  lwd    = c(NA, NA, 2),                          # only the 3rd has width
  bty    = "n"                                    # no box
)

dev.off()
